import { history } from 'umi';
import { fetchOwnerAccess, fetchOtherAdminInfo, fakeChangePassword } from '@/services/UserServices';
import { notification } from 'antd';
const userInfo = {
  namespace: 'userInfo',

  state: {
    currentUser: {},
    flag: 0,
    menuList: [], // 用户菜单列表
    menuBtn: {},
    menuNameObj: {},
    loading: false,
    openedMenu: [], // 保存已经打开的菜单栏 用于顶部tab
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    saveOpenMenu(state, { payload }) {
      const { menuItem } = payload;
      if (!state.openedMenu.find((i) => i.path === menuItem.path)) {
        const openedMenu = [...state.openedMenu];
        openedMenu.push(menuItem);
        return { ...state, openedMenu };
      }
      return state;
    },
  },
  effects: {
    *fetchGetAuthMenuTree({ payload, callback }, { call, put }) {
      const response = yield call(fetchOwnerAccess, payload);
      if (!response) return;
      console.log(yield);
      const { authAccessForUseResponseList } = response.content;
      yield put({
        type: 'save',
        payload: {
          menuList: authAccessForUseResponseList,
        },
      });
      callback && callback(authAccessForUseResponseList);
    },
    *fakeChangePassword({ payload, callback }, { call, put }) {
      const response = yield call(fakeChangePassword, payload);
      if (!response) {
        return;
      }
      notification.success({
        message: '温馨提示',
        description: `修改成功`,
      });
      history.goBack();
    },
    *fetchAdminInfo({ payload, callback }, { call, put }) {
      const response = yield call(fetchOtherAdminInfo, payload);
      if (!response) return;
      const { AuthAdmin } = response.content;
      yield put({
        type: 'save',
        payload: {
          currentUser: authAccessForUseResponseList,
        },
      });
      callback && callback();
    },
  },
};
export default userInfo;
